package com.javaweb.dao.impl;

import com.javaweb.dao.RoleDao;
import com.javaweb.model.Permission;
import com.javaweb.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/24 22:44
 */

/**
 * todo:完成如下所有方法
 */
@Repository
public class RoleDaoImpl implements RoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate ;
    @Override
    public Integer insert(Role role) {
        return null;
    }

    @Override
    public Integer insertRolePerm(Role role, Permission perm) {
        return null;
    }

    @Override
    public List<Role> selectRolesByUserName(String username) {
        return null;
    }

    @Override
    public List<Role> selectRolesByUserId(int userId) {
        String sql = "SELECT tr.role_id AS role_id, tr.name AS role_name " +
                " FROM tb_role tr INNER JOIN tb_user_role ur " +
                " ON tr.role_id = ur.role_id " +
                " WHERE ur.user_id = ?" ;
        return jdbcTemplate.query(sql, new Object[]{userId}, new RowMapper<Role>() {
            @Override
            public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
                return Role.builder()
                        .name(rs.getString("role_name"))
                        .roleId(rs.getInt("role_id"))
                        .build();
            }
        });


    }

    @Override
    public void deleteByRoleId(int roleId) {

    }

    @Override
    public void deleteUserRole(int userId, int roleId) {

    }
}
